# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-bcrypt
pkgver=2.3
_rockrel=1
pkgrel=0
pkgdesc="A Lua wrapper for bcrypt"
url="https://github.com/mikejsavage/lua-bcrypt"
license="MIT"
arch="all"
source="$pkgname-$pkgver-$_rockrel.tar.gz::https://github.com/mikejsavage/lua-bcrypt/archive/refs/tags/v$pkgver-$_rockrel.tar.gz"
builddir="$srcdir/$pkgname-$pkgver-$_rockrel"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v lua$_v-dev lua$_v-libs luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

prepare() {
	default_prepare

	for _v in $_luaversions; do
		cp -r "$builddir" "$builddir-$_v"
	done
}

build() {
	local _v; for _v in $_luaversions; do
		msg "Building for Lua $_v..."
		cd "$builddir-$_v"
		luarocks-$_v \
			CC="$CC" \
			CFLAGS="$CFLAGS -fPIC" \
			LUA_INCDIR="$(pkg-config --variable=includedir "lua$_v")" \
			LUA_LIBDIR="$(pkg-config --variable=libdir "lua$_v")" \
			LUA_VERSION="$_v" \
			make --tree "./build-$_v" \
			--deps-mode=none \
			rockspec/"${pkgname#lua-}-$pkgver-$_rockrel.rockspec"
	done
}

check() {
	local _v; for _v in $_luaversions; do
		msg "Testing Lua $_v..."
		cd "$builddir-$_v"
		lua$_v test-digest.lua
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local _v="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $_v)"
	depends="lua$_v"
	install_if="$pkgname=$pkgver-r$pkgrel lua$_v"
	local libdir="$subpkgdir/usr/lib/lua/$_v/"

	cd "$builddir-$_v"
	mkdir -p "$libdir"
	cp -a bcrypt.so "$libdir"/
}

sha512sums="
1485d9962b0525284e8d9477181388622bee3e4c26680b8638fbf5b988e2c2e57d70cebb907fd319ad159f562ff776ccd55a6c32a3806a45477dc1b59180c374  lua-bcrypt-2.3-1.tar.gz
"
